package com.AJava1710D.设计模式.原型模式;

import java.io.*;
import java.util.List;
import java.util.Map;

/**
 * 作者: LCG
 * 日期: 2020/6/28 11:46
 * 描述:
 */
public class TestABA implements Cloneable,Serializable {

    public TestABA() {
        System.out.println("=====执行构造====");
    }

    private int age;

    private String userName;

    private List<Map<String,Object>> list;


    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public List<Map<String, Object>> getList() {
        return list;
    }

    public void setList(List<Map<String, Object>> list) {
        this.list = list;
    }

    /**
     * 浅克隆
     * @return
     */
    public TestABA getClone() {
        Object clone = null;
        try {
            clone = super.clone();
        }catch (Exception e){
            e.printStackTrace();
        }
        return (TestABA)clone;
    }


    //深复制
    public TestABA deepClone() throws IOException, ClassNotFoundException {

        ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream=new ObjectOutputStream(byteArrayOutputStream);

        objectOutputStream.writeObject(this);

        //重新写出
        ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        ObjectInputStream objectInputStream=new ObjectInputStream(byteArrayInputStream);

        TestABA testABA = (TestABA) objectInputStream.readObject();

        objectInputStream.close();
        byteArrayInputStream.close();
        objectOutputStream.close();
        byteArrayOutputStream.close();

        return testABA;

    }

}
